package cn.hou.java.designpattern._01factorycacl;

import java.util.Scanner;
import java.util.regex.Pattern;

//使用工厂模式开发一个简单计算器
public class CaclTest {
	public static void main(String[] args) {
		System.out.println("请输入第一个数字:");
		Scanner scanner=new Scanner(System.in);
		String num1 = scanner.nextLine();
		System.out.println("请输入运算符(+-*/):");
		String oper = scanner.nextLine();
		System.out.println("请输入第二个数字:");
		String num2 = scanner.nextLine();
		//通过工厂获取运算类
		Operator operator = OperatorFactory.getOperator(oper);
		if(operator!=null){
			if(isDouble(num1) && isDouble(num2)){
				double result = operator.getResult(Double.parseDouble(num1), Double.parseDouble(num2));
				System.out.println("结果是:"+result);							
			}else{				
				System.out.println("请输入正确的数字!");							
			}
		}
	}
	//判断浮点数（double和float）
	private static boolean isDouble(String str) {
		if (null == str || "".equals(str)) {
			return false;
		}
		Pattern pattern = Pattern.compile("^[0-9]+(.[0-9]+)?$");//数字,包括整数和浮点
		return pattern.matcher(str).matches();
	}
}
